package com.baizhi.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baizhi.dao.PmsBrandMapper;
import com.baizhi.dao.PmsCategoryBrandRelationMapper;
import com.baizhi.entity.PmsBrand;
import com.baizhi.entity.PmsCategoryBrandRelation;
import com.baizhi.service.IPmsBrandService;
import com.baizhi.vo.PmsBrandVo;
import com.baizhi.vo.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lby
 * @since 2020-09-24
 */
@Service
public class PmsBrandServiceImpl extends ServiceImpl<PmsBrandMapper, PmsBrand> implements IPmsBrandService {
    @Autowired
    private PmsBrandMapper pmsBrandMapper;
    @Autowired
    private PmsCategoryBrandRelationMapper pmsCategoryBrandRelationMapper;
    @Override
    public void addPmsBrand(PmsBrand pmsBrand) {
        pmsBrandMapper.insert(pmsBrand);
    }

    @Override
    public Workbook downloadExcel() {
        List<PmsBrand> list = list();
        Workbook sheets = ExcelExportUtil.exportExcel(
                new ExportParams("所有品牌数据", "list"),
                PmsBrand.class,
                list);
        return  sheets;
    }

    @Override
    public Map selectBrandBySize(Integer page, Integer limit) {
        Page<PmsBrand> brandPage = page(new Page<>(page, limit));

        List<PmsBrand> brands = brandPage.getRecords();
        /**
         * 需要将PmsBrand 转化 PmsBrandVo
         *
         * Vo需要添加类名名称 ---》查询关系表
         */
        List<PmsBrandVo> list = brands.stream()
                .map(pmsBrand -> {
                    PmsBrandVo brandVo = new PmsBrandVo();

                    BeanUtils.copyProperties(pmsBrand,brandVo);

                    PmsCategoryBrandRelation categoryBrandRelation = pmsCategoryBrandRelationMapper.selectOne(
                            new QueryWrapper<PmsCategoryBrandRelation>().eq("brand_id", pmsBrand.getBrandId())
                    );

                    brandVo.setCategoryName(categoryBrandRelation.getCategoryName());

                    return brandVo;
                })
                .collect(Collectors.toList());
        return null;
    }

    @Override
    public R selectBrandBySizeR(Integer page, Integer limit) {
        Page<PmsBrand> brandPage = page(new Page<>(page, limit));

        List<PmsBrand> brands = brandPage.getRecords();
        /**
         * 需要将PmsBrand 转化 PmsBrandVo
         *
         * Vo需要添加类名名称 ---》查询关系表
         */
        List<PmsBrandVo> list = brands.stream()
                .map(pmsBrand -> {
                    PmsBrandVo brandVo = new PmsBrandVo();

                    BeanUtils.copyProperties(pmsBrand,brandVo);

                    PmsCategoryBrandRelation categoryBrandRelation = pmsCategoryBrandRelationMapper.selectOne(
                            new QueryWrapper<PmsCategoryBrandRelation>().eq("brand_id", pmsBrand.getBrandId())
                    );

                    brandVo.setCategoryName(categoryBrandRelation.getCategoryName());

                    return brandVo;
                })
                .collect(Collectors.toList());

        return R.ok().put("count",brandPage.getTotal()).put("data",list);
    }
}
